package com.zidian.leader.activity;

import android.content.Intent;
import android.os.Bundle;
import android.support.design.widget.Snackbar;
import android.support.v7.widget.LinearLayoutManager;
import android.support.v7.widget.RecyclerView;

import com.zidian.leader.R;
import com.zidian.leader.adapter.StudentStatisticsAdapter;
import com.zidian.leader.api.HttpFactory;
import com.zidian.leader.api.HttpResultFunc;
import com.zidian.leader.common.activity.MySwipeBackActivity;
import com.zidian.leader.common.util.HawkUtils;
import com.zidian.leader.common.util.StringConstants;
import com.zidian.leader.entity.EvaluateRateCollege;
import com.zidian.leader.entity.StudentStatisticsBean;

import java.util.ArrayList;
import java.util.List;

import butterknife.Bind;
import rx.Subscriber;
import rx.android.schedulers.AndroidSchedulers;
import rx.functions.Func1;
import rx.schedulers.Schedulers;

/**
 * 评教完成率（学院）Activity
 * Created by GongCheng on 2017/2/21.
 */

public class EvaluateRateCollegeActivity extends MySwipeBackActivity {
    @Bind(R.id.recycler_view)
    RecyclerView recyclerView;

    private StudentStatisticsAdapter adapter;
    private String college;
    private int sum;
    private int num;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        Intent intent = getIntent();
        college = intent.getStringExtra("college");
        sum = intent.getIntExtra("sum", 0);
        num = intent.getIntExtra("num", 0);
        initView();
    }

    @Override
    protected int getLayout() {
        return R.layout.activity_base_recycler_view;
    }

    private void initView() {
        actionBar.setTitle(college);
        actionBar.setHomeAsUpIndicator(R.drawable.ic_arrow_back_24dp);
        adapter = new StudentStatisticsAdapter(this);
        recyclerView.setLayoutManager(new LinearLayoutManager(this));
        recyclerView.setAdapter(adapter);
        getEvaluateRateCollege();
    }

    private void getEvaluateRateCollege() {
        subscription = HttpFactory.getInstance().getEvaluateRateCollege(
                college, HawkUtils.getUsername(),
                HawkUtils.getRoleId() + StringConstants.STRING_EMPTY,
                HawkUtils.getToken())
                .map(new HttpResultFunc<List<EvaluateRateCollege>>())
                .map(new Func1<List<EvaluateRateCollege>, List<StudentStatisticsBean>>() {
                    @Override
                    public List<StudentStatisticsBean> call(List<EvaluateRateCollege> evaluateRateColleges) {
                        List<StudentStatisticsBean> list = new ArrayList<>();
                        list.add(new StudentStatisticsBean("全院平均教评完成率", sum, num));
                        for (int i = 0; i < evaluateRateColleges.size(); i++) {
                            list.add(new StudentStatisticsBean(
                                    evaluateRateColleges.get(i).getMajorName(),
                                    evaluateRateColleges.get(i).getMaxEvaluateNum(),
                                    evaluateRateColleges.get(i).getRealEvaluateNum()
                            ));
                        }
                        return list;
                    }
                })
                .subscribeOn(Schedulers.io())
                .unsubscribeOn(Schedulers.io())
                .observeOn(AndroidSchedulers.mainThread())
                .subscribe(new Subscriber<List<StudentStatisticsBean>>() {
                    @Override
                    public void onStart() {
                        super.onStart();
                        progressDialog.show();
                    }

                    @Override
                    public void onCompleted() {
                        progressDialog.dismiss();
                    }

                    @Override
                    public void onError(Throwable e) {
                        progressDialog.dismiss();
                        Snackbar.make(recyclerView, e.getMessage(), Snackbar.LENGTH_SHORT).show();
                    }

                    @Override
                    public void onNext(List<StudentStatisticsBean> list) {
                        adapter.setDate(list);
                    }
                });
    }
}
